package com.hhit.zhgd.controller

import com.hhit.zhgd.domain.entity.Menu
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.MenuService
import com.hhit.zhgd.utils.menuToTree
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import javax.validation.constraints.Min

@Validated
@RestController
class MenuController {
    @Autowired
    private val menuService: MenuService? = null

    @GetMapping("/menus")
    fun getMenuList(name: String, status: Int): BaseResp<List<HashMap<String, Any?>>?> {
        val params = HashMap<String, Any?>()
        params["name"] = name
        params["status"] = status

        val all = menuService?.getAll(params)
        return if (all != null) {
            val menuToTree = menuToTree(all, 0)
            BaseResp.success(menuToTree)
        } else {
            BaseResp.success(listOf())
        }
    }

    @GetMapping("/menu/{id}")
    fun getMenuInfo(@PathVariable("id") @Min(value = 1, message = "参数错误") menuId: Int): BaseResp<Menu?> {
        return BaseResp.success(menuService?.getById(menuId))
    }

//    @PostMapping("/menu")
//    fun addMenu(@Validated(MenuDTO.Save::class) menuDTO: MenuDTO): BaseResp<Any?> {
//        val menu = Menu(
//            id = menuDTO.id,
//            name = menuDTO.name,
//            parentId = menuDTO.parent_id?.toString()?.toLong(),
//            type = menuDTO.type,
//            path = menuDTO.path,
//            perms = menuDTO.perms,
//            icon = menuDTO.icon,
//            sort = menuDTO.sort,
//            status = menuDTO.status
//        )
//
//        if ()
//
//    }




    private fun checkParams(menu:Menu):HashMap<String,Any?> {
        val result: HashMap<String,Any?> = HashMap()
        result["status"] = false

        if (menu.type.equals("C")) {
            // 菜单必填path、perms、icon
            if (menu.path == null || menu.path.length === 0) {
                result["message"] = "请输入组件路径"
                return result
            }
            if (menu.perms == null || menu.perms.length === 0) {
                result["message"] = "请输入权限标识"
                return result
            }
        } else if (menu.type.equals("F")) {
            // 按钮必填perms
            if (menu.perms == null || menu.perms.length === 0) {
                result["message"] = "请输入权限标识"
                return result
            }
        } else if (menu.type.equals("M")) {
            // 目录必填icon
            if (menu.icon == null || menu.icon.length === 0) {
                result["message"] = "请输入菜单图标"
                return result
            }
        }

        result["data"] = menu
        result["status"] = true
        return result
    }
}















